package com.zhj.java8.stream;

import com.zhj.java8.bean.Student;

import java.util.*;
import java.util.stream.Collectors;

public class TerminationStreamDemo {

    public static void main(String[] args) {
        List<Student> students = new ArrayList<>();
        students.add(new Student(1,"小华",23,1));
        students.add(new Student(2,"小米",20,2));
        students.add(new Student(3,"小果",30,3));
        students.add(new Student(4,"小维",18,2));
        students.add(new Student(5,"小华",23,2));
        System.out.println("--------------------匹配聚合操作--------------------");
        // allMatch：接收一个 Predicate 函数，当流中每个元素都符合该断言时才返回true，否则返回false
        boolean allMatch = students.stream().allMatch(stu -> stu.getAge() > 10);
        System.out.println("全部符合大于10岁条件：" + allMatch);
        // noneMatch：接收一个 Predicate 函数，当流中每个元素都不符合该断言时才返回true，否则返回false
        boolean noneMatch = students.stream().noneMatch(stu -> stu.getAge() > 10);
        System.out.println("全部不符合大于10岁条件：" + noneMatch);
        // anyMatch：接收一个 Predicate 函数，只要流中有一个元素满足该断言则返回true，否则返回false
        boolean anyMatch = students.stream().anyMatch(stu -> stu.getAge() > 20);
        System.out.println("含有任意符合大于20岁条件：" + anyMatch);

        // findFirst：返回流中第一个元素
        Student findFirst = students.stream().findFirst().get();
        System.out.println("第一个学生：" + findFirst);
        // findAny：返回流中的任意元素
        Student findAny = students.stream().findAny().get();
        System.out.println("任意一个学生：" + findAny);

        //  count：返回流中元素的总个数
        long count = students.stream().count();
        System.out.println("学生总数：" + count);
        // max：返回流中元素最大值
        Student max = students.stream().max(Student::compareTo).get();
        System.out.println("年龄最大学生：" + max);
        // max：返回流中元素最大值
        Student min = students.stream().min(Student::compareTo).get();
        System.out.println("年龄最小学生：" + min);

        System.out.println("--------------------规约操作--------------------");
        System.out.println("学生年龄总和：" + students.stream().map(Student::getAge).reduce(Integer::sum));
        System.out.println("学生年龄最大：" + students.stream().map(Student::getAge).reduce(Integer::max));

        System.out.println("--------------------收集操作--------------------");
        List<Student> list = students.stream().collect(Collectors.toList());
        Set<Student> set = students.stream().collect(Collectors.toSet());
        Map<Integer, String> map = students.stream().collect(Collectors.toMap(Student::getId, Student::getName));
        String joinName = students.stream().map(Student::getName).collect(Collectors.joining(",", "(", ")"));
        // 总数
        students.stream().collect(Collectors.counting());
        // 最大年龄
        students.stream().map(Student::getAge).collect(Collectors.maxBy(Integer::compare)).get();
        // 年龄和
        students.stream().collect(Collectors.summingInt(Student::getAge));
        // 平均年龄
        students.stream().collect(Collectors.averagingDouble(Student::getAge));

        // 信息合集
        DoubleSummaryStatistics statistics = students.stream().collect(Collectors.summarizingDouble(Student::getAge));
        System.out.println("count:" + statistics.getCount() + ",max:" + statistics.getMax() + ",sum:" + statistics.getSum() + ",average:" + statistics.getAverage());

        // 分组
        Map<Integer, List<Student>> collect = students.stream().collect(Collectors.groupingBy(Student::getSex));
        System.out.println(collect);
        //多重分组,先根据性别分再根据年龄分
        Map<Integer, Map<Integer, List<Student>>> typeAgeMap = list.stream().collect(Collectors.groupingBy(Student::getSex, Collectors.groupingBy(Student::getAge)));

        //分区
        //分成两部分，一部分大于20岁，一部分小于等于20岁
        Map<Boolean, List<Student>> partMap = list.stream().collect(Collectors.partitioningBy(v -> v.getAge() > 20));

        //规约
        Integer allAge = list.stream().map(Student::getAge).collect(Collectors.reducing(Integer::sum)).get();
        System.out.println(allAge);
    }
}
